﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Modules_News_Edit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
        // this.btnCancel.Attributes.Add("onclick", "return ClearAll()");
        if (!this.IsPostBack)
        {
            InitItem();
            LoadGrdDetail();
        }
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();

    }

    private void LoadGrdDetail()
    {
        try
        {
            int ID = -1;
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
            WebData db = new WebData();
            this.grdDs.DataSource = db.tbl_Category_Langs.GetListByIdPrimary(ID);
            this.grdDs.DataBind();
        }
        catch
        {
        }
    }

    private void AddNew()
    {
        String strMess = "";

        try
        {
            WebData db = new WebData();
            DataLibrary.tbl_Category item = new DataLibrary.tbl_Category();
            item.Description = txtDescription.Text;
            item.OrderId = Convert.ToInt32(txtorderId.Text);
            item.ShowTop = chkShowTop.Checked;
            int i = db.tbl_Categorys.UpdateOnSubmit(item);
            strMess = i == -1 ? Message.Update_err : Message.Update;

        }
        catch (Exception err)
        {
            strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
        }

        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);


    }

    private void InitItem()
    {
        int ID = -1;
        try
        {
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
        }
        catch { }
        ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        WebData db = new WebData();
        DataLibrary.tbl_Category item = db.tbl_Categorys.GetItemById(ID);
        if (item != null)
        {
            txtDescription.Text = item.Description;
            txtorderId.Text = item.OrderId.ToString();
            chkShowTop.Checked = item.ShowTop;
        }
    }


    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdDs.EditIndex = -1;
        LoadGrdDetail();
    }

    protected void grdDs_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            String str = e.CommandName.ToString();
            if (str == "Del")
            {
                Label mylbID = (Label)grdDs.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("lblID");
                int id = Convert.ToInt32(mylbID.Text);
                WebData db = new WebData();
                DataLibrary.tbl_Category_Lang item = new DataLibrary.tbl_Category_Lang();
                item.IdCatLang = id;
                int i = db.tbl_Category_Langs.DeleteOnSubmit(item);
                if (i != -1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Delete + "');", true);
                    LoadGrdDetail();
                }
                else
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Delete_err + "');", true);

            }
            else
                if (str.Equals("AddNew"))
                {
                    int i = -1;
                    TextBox txtName = (TextBox)this.grdDs.FooterRow.FindControl("NewnameCat");
                    TextBox txtNewUrl = (TextBox)this.grdDs.FooterRow.FindControl("NewUrl");
                    TextBox txtNewDescription = (TextBox)this.grdDs.FooterRow.FindControl("NewDescription");
                    DropDownList cmbNewType = (DropDownList)grdDs.FooterRow.FindControl("cmbNewLang");

                    int ID = -1;
                    try
                    {
                        ID = Convert.ToInt32(Request.Params["ID"].ToString());
                    }
                    catch { }
                    DataLibrary.tbl_Category_Lang item = new DataLibrary.tbl_Category_Lang();
                    item.Description = txtNewDescription.Text;
                    item.IdCat = ID;
                    item.IdLang = 1;
                    item.IdCatLang = 1;
                    item.NameCat = txtName.Text;
                    item.Url = txtNewUrl.Text;
                    i = data.tbl_Category_Langs.InsertOnSubmit(item);
                    if (i != -1)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Insert + "');", true);
                        this.LoadGrdDetail();
                    }
                    else
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Insert_err + "');", true);
                }
        }
        catch (Exception err)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + err.Message.ToString() + "');", true);
        }
    }

    WebData data = new WebData();
    protected void grdDs_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton LBtnDelete = (LinkButton)e.Row.Cells[0].FindControl("btnDelete");
                LBtnDelete.Attributes.Add("onclick", "return UserConfirmation();");
                LBtnDelete.CommandArgument = e.Row.RowIndex.ToString();
                //Combobox
                DropDownList cmbType = (DropDownList)e.Row.FindControl("cmbLang");
                if (cmbType != null)
                {
                    cmbType.DataSource = data.tbl_Languages.GetAll();
                    cmbType.DataBind();
                    cmbType.SelectedValue = grdDs.DataKeys[e.Row.RowIndex].Values[1].ToString();
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                DropDownList cmbNewType = (DropDownList)e.Row.FindControl("cmbNewLang");
                cmbNewType.DataSource = data.tbl_Languages.GetAll();
                cmbNewType.DataBind();
            }

        }
        catch (Exception err)
        {

            ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + err.Message.ToString() + "');", true);
        }

    }
    protected void grdDs_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.grdDs.EditIndex = e.NewEditIndex;
        this.LoadGrdDetail();
    }
    protected void grdDs_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.grdDs.EditIndex = -1;
        this.LoadGrdDetail();
    }

    protected void grdDs_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            int i = -1;
            TextBox txtName = (TextBox)this.grdDs.Rows[e.RowIndex].FindControl("txtnameCat");
            TextBox txtNewUrl = (TextBox)this.grdDs.Rows[e.RowIndex].FindControl("txtUrl");
            TextBox txtNewDescription = (TextBox)this.grdDs.Rows[e.RowIndex].FindControl("txtDescription");
            DropDownList cmbNewType = (DropDownList)grdDs.Rows[e.RowIndex].FindControl("cmbLang");

            int ID = -1;
            try
            {
                ID = Convert.ToInt32(Request.Params["ID"].ToString());
            }
            catch { }
            DataLibrary.tbl_Category_Lang item = new DataLibrary.tbl_Category_Lang();
            item.Description = txtNewDescription.Text;
            item.IdCat = ID;
            item.IdLang = 1;
            item.NameCat = txtName.Text;
            item.Url = txtNewUrl.Text;
            item.IdCatLang = Convert.ToInt32(this.grdDs.DataKeys[this.grdDs.Rows[e.RowIndex].RowIndex].Value);
            i = data.tbl_Category_Langs.UpdateOnSubmit(item);

            if (i != -1)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Update + "');", true);

            }
            else
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Update_err + "');", true);
        }
        catch { }
        this.grdDs.EditIndex = -1;
        this.LoadGrdDetail();
    }
    protected void btnCancel_Click1(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }
}